import {fetch} from '../../../utils/axios'
import {downExcel} from '../../../utils/common'

const state = {
  name: '服务器列表',
  editdialog: false,
  id: null,
  search: '',
  chosedata: {},
  totalItems: 0,
  items: [],
  loader: null,
  loading: true,
  loadsync: false,
  pagination: {},
  headers: [],
  tempHeader: [],
  etext: 'No Data',
  rowsPerPageItems: [10, 50, 100, { text: 'All', value: 100000 }],
  searchLabel: '主机名，ip，mac,固定资产编号，快速服务代码，机器编号',
  status: {},
  type: {},
  env: {},
  clouds: {},
  re_per: {},
  smenu: false,
  sdate: null,
  emenu: false,
  edate: null,
  modal: false
}

const mutations = {
  handlerHost: (state) => {
    const { sortBy, descending, page, rowsPerPage } = state.pagination
    let ordering = descending ? '-' + sortBy : sortBy
    fetch('/hosts/', {
      offset: (page - 1) * rowsPerPage,
      limit: rowsPerPage,
      ordering,
      search: state.search,
      status: state.status.value,
      asset_type: state.type.value,
      env__env: state.env.value,
      env__clouds: state.clouds.value,
      create_time_after: state.sdate,
      create_time_before: state.edate
    }).then((response) => {
      state.items = response.results
      state.totalItems = response.count
      state.loading = false
    }).catch((error) => {
      state.etext = error.response
    })
  },
  clearHostList: (state, payload) => {
    payload.reset()
  },
  hostDownload () {
    downExcel(state.headers, state.items)
  },
  openHostEdit (state, payload) {
    state.id = payload.id
    state.editdialog = true
  },
  loadBtn () {
    state.loadsync = true
    fetch('/hosts/host_sync/', {
    }).then((response) => {
      alert(JSON.parse(response))
      state.loadsync = false
      this.loader = null
      this.dispatch('listhost/handlerHost')
    }).catch((error) => {
      alert(error.response.data)
      state.loadsync = false
      this.loader = null
    })
  },
  loadHostFilters (state) {
    fetch('/hosts/host_filters/', {
    }).then((response) => {
      let res = JSON.parse(response)
      state.status.items = res.status
      state.type.items = res.type
      state.env.items = res.env
      state.clouds.items = res.clouds
      state.re_per.items = res.re_per
    }).catch((error) => {
      state.etext = error.response
    })
  },
  loadHostHeaders (state) {
    fetch('/getmodels/', {
      appname: 'cmdb',
      modelname: 'Host'
    }).then((response) => {
      state.headers = JSON.parse(response)
      state.tempHeader = JSON.parse(response)
      state.tempHeader.splice(6, 0, {text: '关联项目', align: 'center', value: 'prjname', sortable: false})
      state.tempHeader.splice(7, 0, {text: '关联服务', align: 'center', value: 'srvname', sortable: false})
      state.tempHeader.splice(8, 0, {text: '运行版本', align: 'center', value: 'version', sortable: false})
      state.headers.splice(6, 0, {text: '关联项目', align: 'center', value: 'prjname', sortable: false})
      state.headers.splice(7, 0, {text: '关联服务', align: 'center', value: 'srvname', sortable: false})
      state.headers.splice(8, 0, {text: '运行版本', align: 'center', value: 'version', sortable: false})
      state.headers.splice(0, 0, {text: '来源', align: 'center', sortable: false})
    }).catch((error) => {
      state.etext = error.response
    })
  }
}

const actions = {
  handlerHost: ({commit}) => {
    state.loading = true
    commit('handlerHost')
  },
  hostSearch ({commit, state}) {
    state.loading = true
    commit('handlerHost')
  },
  clearHostList ({commit}, payload) {
    commit('clearHostList', payload)
  },
  hostDownload ({commit}) {
    commit('hostDownload')
  },
  openHostEdit ({commit}, payload) {
    commit('openHostEdit', payload)
  },
  loadBtn ({commit}) {
    commit('loadBtn')
  },
  loadHostFilters ({commit}) {
    commit('loadHostFilters')
  },
  loadHostHeaders ({commit}) {
    commit('loadHostHeaders')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
